Repository navigation
Limit non-hibernatable WebSocket connections per Durable Object - #8169
Conversation
|
Open questions: |
4480c17 to
6d684b7
Compare
There was a problem hiding this comment.
Superseded by the review of
6d684b73.
This review, of 4480c176
💬 Approved with comments — The limit enforcement and release logic is correct. One optional suggestion: add test coverage for the kill-switch autogate path.
Findings: 1 suggestion (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 suggestion
The limit logic is correct. Nothing tests the kill-switch path.
Recommendation: 💬 Optional Nits
- [SUGGESTION] No test covers the DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH path (
src/workerd/api/tests/BUILD.bazel:1215)
View full review
The limit is checked before any state changes. Hibernatable WebSockets are excluded from the count. Each WebSocket gives its slot back from the Accepted destructor through a refcounted counter, so a WebSocket that outlives its actor still releases its slot safely.
One optional gap: the new test target turns off the @all-autogates variant, so the case where the kill switch removes the limit has no test.
✅ All other checks passed
🔒 Security: ✅ LGTM
The change reduces a resource-exhaustion risk and doesn't weaken any security boundary. The counter is only touched on the isolate thread, so the non-atomic kj::Rc is safe. The kill switch is set per process and can't be set from untrusted input.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions. The compatibility decision is recorded in the commit message, as the backward-compatibility rules require.
⚡ Performance: ✅ LGTM
No performance concerns. The check and the counter updates are O(1).
📝 Documentation: ✅ LGTM
The comments on the limit constant and the autogate accurately describe the behavior.
Reviewed commit: 4480c176
There was a problem hiding this comment.
Superseded by the review of
f51da278.
This review, of 6d684b73
💬 Approved with comments — The limit enforcement and release logic is correct. One optional suggestion: add test coverage for the kill-switch autogate path.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 1 suggestion
The limit logic is correct. Nothing tests the kill-switch path.
Recommendation: 💬 Optional Nits
- [SUGGESTION] No test covers the DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH path (
src/workerd/api/tests/BUILD.bazel:1215)
View full review
The limit is checked before any state changes. Hibernatable WebSockets are excluded from the count. Each WebSocket gives its slot back from the Accepted destructor through a refcounted counter, so a WebSocket that outlives its actor still releases its slot safely.
One optional gap: the new test target turns off the @all-autogates variant, so the case where the kill switch removes the limit has no test.
✅ All other checks passed
🔒 Security: ✅ LGTM
The change reduces a resource-exhaustion risk and doesn't weaken any security boundary. The counter is only touched on the isolate thread, so the non-atomic kj::Rc is safe. The kill switch is set per process and can't be set from untrusted input.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions. The compatibility decision is recorded in the commit message, as the backward-compatibility rules require.
⚡ Performance: ✅ LGTM
No performance concerns. The check and the counter updates are O(1).
📝 Documentation: ✅ LGTM
The comments on the limit constant and the autogate accurately describe the behavior.
Reviewed commit: 6d684b73 — same changes as 4480c176, so this is that commit's review (no new review was run; line numbers are as of 4480c176)
apeacock1991
left a comment
There was a problem hiding this comment.
LGTM - left a comment agreeing with the AI reviewer on one point
My 2c: A) Agree with you, don't think it should be but might be worth checking if there are cases of actors with 32k+ connections for non-hibernated WebSockets just in case someone is somehow operating at say 40k fine and we break their app (seems unlikely) |
6d684b7 to
f51da27
Compare
There was a problem hiding this comment.
Superseded by the review of
46d3b421.
This review, of f51da278
✅ Approved — No issues found. The earlier suggestion to test the kill-switch path is fixed by the new kill-switch test.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The change reduces a resource-exhaustion risk and doesn't weaken any security boundary.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions. The commit message records the compatibility decision, as the backward-compatibility rules require.
📋 Code Quality: ✅ LGTM
The limit is checked before any state changes in both internalAccept() and initConnection(). The kill-switch test accepts more WebSockets than the limit allows with the autogate on, so it covers the case where the limit is turned off.
⚡ Performance: ✅ LGTM
The check and the counter updates are O(1).
📝 Documentation: ✅ LGTM
The comments on the limit constant, the counter and the autogate accurately describe the behavior.
Reviewed commit: f51da278
Added tests for killswitch case. |
How much tricker is combining the limit out of curiosity? mostly because it's easier to reason about if it's just "~32k websocket limit" vs it being split (but fine to merge as-is if it's tricky, or if we think split is better for some reason) |
A Durable Object instance could accept or open any number of non-hibernatable WebSockets, all on the one machine that hosts it. Each uses memory and connection-handling capacity that the machine shares with every other Durable Object on it. One instance holding tens of thousands, whose clients all reconnect at once when it restarts, can exhaust those resources and starve its neighbors. Hibernatable WebSockets are already limited to 32768 per instance. Limit each instance to 32768 non-hibernatable WebSockets, counted separately from its hibernatable ones. A WebSocket counts from when it is accepted, by accept() or when a connection made with `new WebSocket(url)` opens, until it is released. Past the limit, accept() throws an Error. A connection made with `new WebSocket(url)` is closed once it opens, and the WebSocket reports the Error in its error event. That check runs before the WebSocket takes the connection, so that an error listener that throws can't keep it open. The count is refcounted, since a WebSocket can give back its slot after its actor is destroyed. The DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH autogate removes the limit. Unlike most autogates, enabling it turns the change off, so the limit applies wherever this is deployed. The limit's test has no @all-autogates variant, and a separate test checks that the kill switch lets an actor accept more WebSockets than the limit. This changes behavior for existing Workers without a compatibility flag: a Durable Object that holds more than 32768 non-hibernatable WebSockets starts seeing errors. A flag would leave existing Durable Objects unlimited, which defeats the purpose, so we accept the risk, with the kill switch as the fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f51da27 to
46d3b42
Compare
There was a problem hiding this comment.
✅ Approved — No issues found. The earlier suggestion to test the kill-switch path is fixed by the new kill-switch test.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The change reduces a resource-exhaustion risk and doesn't weaken any security boundary.
📖 AGENTS.md: ✅ LGTM
The change follows the repository's conventions. The commit message records the compatibility decision, as the backward-compatibility rules require.
📋 Code Quality: ✅ LGTM
The limit is checked before any state changes in both internalAccept() and initConnection(). The kill-switch test accepts more WebSockets than the limit allows with the autogate on, so it covers the case where the limit is turned off.
⚡ Performance: ✅ LGTM
The check and the counter updates are O(1).
📝 Documentation: ✅ LGTM
The comments on the limit constant, the counter and the autogate accurately describe the behavior.
Reviewed commit: 46d3b421 — same changes as f51da278, so this is that commit's review (no new review was run; line numbers are as of f51da278)
Keeping separate for purpose of this MR per comment on https://jira.cfdata.org/browse/STOR-5843. We can keep combine the limits as a follow up change if desired. |
Both timed out at the 15s "medium" timeout in a downstream debug build, on a change that touched neither: * websocket-accept-limit-test, added in cloudflare#8169, accepts the 32768 WebSockets of the limit. Its test case passed after 13.5s, and starting and stopping workerd took it past 15s. * tail-worker-test@all-autogates runs several other tests' workers to trace them. Recent debug builds took 7.2s to 12.5s of its 15s, and a slower executor timed it out before its first test case. "large" gives them 60s. On bazelflare it also runs each on an executor of its own (test_exec_properties).
A Durable Object instance could accept or open any number of non-hibernatable WebSockets, all on the one machine that hosts it. Each uses memory and connection-handling capacity that the machine shares with every other Durable Object on it. One instance holding tens of thousands, whose clients all reconnect at once when it restarts, can exhaust those resources and starve its neighbors. Hibernatable WebSockets are already limited to 32768 per instance.
Limit each instance to 32768 non-hibernatable WebSockets, counted separately from its hibernatable ones. A WebSocket counts from when it is accepted, by accept() or when a connection made with
new WebSocket(url)opens, until it is released. Past the limit, accept() throws an Error. A connection made withnew WebSocket(url)is closed once it opens, and the WebSocket reports the Error in its error event.The DURABLE_OBJECT_WEBSOCKET_LIMIT_KILLSWITCH autogate turns off the limit.
This changes behavior for existing Workers without a compatibility flag: a Durable Object that holds more than 32768 non-hibernatable WebSockets starts seeing errors. A flag would leave existing Durable Objects unlimited, which defeats the purpose, so we accept the risk, with the kill switch as the fallback.